package com.zimo.gui.order;
import com.zimo.utils.FindClass;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;


public class Help implements Order {
    @Override
    public void handle(String[] p) {
        if (p.length > 1) System.out.println("参数个数不正确");
        //打印指定的help方法
        if (p.length == 1){
            //首字母大写
            p[0] = p[0].substring(0, 1).toUpperCase() + p[0].substring(1);
            String packageName = "com.zimo.gui.order."+p[0];
            invokeHelp(packageName,"help");
            return;
        }
        //打印所有类的help方法
        if (p.length == 0) {
            //扫描该包下的所有的类
            FindClass findClass = new FindClass();
            List<String> clazzName = findClass.getClazzName("com.zimo.gui.order",false);

            //调用help方法
            for (String s : clazzName) {
                invokeHelp(s,"describe");
            }

        }
    }

    private void invokeHelp(String packageName,String methodName){
        Class<?> aClass = null;
        try {
            aClass = Class.forName(packageName);
            Method method = aClass.getMethod(methodName);
            //获取构造方法然后newInstance得到的实例传入invoke
            Constructor<?>[] declaredConstructors = aClass.getDeclaredConstructors();
            for (Constructor<?> declaredConstructor : declaredConstructors) {
                method.invoke(declaredConstructor.newInstance());
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void describe() {
    }

    @Override
    public void help() {
    }


}
